package org.jeecg.modules.work.sweep.controller;

import java.sql.Struct;
import java.util.Arrays;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.constant.BikeConstant;
import org.jeecg.modules.utils.RandomCodeUtils;
import org.jeecg.modules.work.dispatch.entity.BikeWorkDispatch;
import org.jeecg.modules.work.repair.entity.BikeWorkRepair;
import org.jeecg.modules.work.repair.service.IBikeWorkRepairService;
import org.jeecg.modules.work.sweep.entity.BikeWorkSweep;
import org.jeecg.modules.work.sweep.mapper.BikeWorkSweepMapper;
import org.jeecg.modules.work.sweep.service.IBikeWorkSweepService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 保洁工单
 * @Author: 月月鸟
 * @Date:   2021-05-31
 * @Version: V1.0
 */
@Api(tags="保洁工单")
@RestController
@RequestMapping("/sweep/bikeWorkSweep")
@Slf4j
public class BikeWorkSweepController extends JeecgController<BikeWorkSweep, IBikeWorkSweepService> {
	@Autowired
	private IBikeWorkSweepService bikeWorkSweepService;

	@Autowired
	private IBikeWorkRepairService bikeWorkRepairService;
	/**
	 * 分页列表查询
	 *
	 * @param bikeWorkSweep
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="保洁工单-分页列表查询", notes="保洁工单-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(BikeWorkSweep bikeWorkSweep,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BikeWorkSweep> queryWrapper = QueryGenerator.initQueryWrapper(bikeWorkSweep, req.getParameterMap());
		Page<BikeWorkSweep> page = new Page<BikeWorkSweep>(pageNo, pageSize);
		IPage<BikeWorkSweep> pageList = bikeWorkSweepService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	 /**
	  * 保洁工单拼接
	  *
	  * @param bikeWorkSweep
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @ApiOperation(value="保洁工单拼接-分页列表查询", notes="保洁工单拼接-分页列表查询")
	 @GetMapping(value = "/pageList")
	 public Result<?> pageList(BikeWorkSweep bikeWorkSweep,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {

		 return	bikeWorkSweepService.pageList(pageNo,pageSize,bikeWorkSweep);
	 }
	 /**
	  *  根据id 派单
	  *
	  * @param webData
	  * @return
	  */
	 @AutoLog(value = "保洁工单-派单",operateType = 3)
	 @ApiOperation(value="保洁工单-派单", notes="保洁工单-派单")
	 @PutMapping(value = "/dispatch")
	 public Result<?> dispatch(@RequestBody BikeWorkSweep webData) {
		 BikeWorkSweep bikeWorkSweep = new BikeWorkSweep();
		 bikeWorkSweep.setId(webData.getId());
		 String yn = bikeWorkSweepService.judgeDispatch(bikeWorkSweep);
		 if (yn.equals(BikeConstant.y_n_yes)){
			 bikeWorkSweep.setIsDispatch(BikeConstant.y_n_yes);
			 bikeWorkSweep.setDispatchTime(new Date());
			 bikeWorkSweepService.updateById(bikeWorkSweep);
			 return Result.OK("派单成功!");
		 }else {
		 	return Result.error("不可重复派单");
		 }

	 }

	 /**
	  * 保洁工单拼接 查看
	  *
	  * @param bikeWorkSweep
	  * @param id
	  * @return
	  */
	 @ApiOperation(value="保洁工单拼接-查看", notes="保洁工单拼接-查看")
	 @GetMapping(value = "/pageListByList")
	 public Result<?> pageListByList(@RequestParam String id ,  BikeWorkSweep bikeWorkSweep) {

		 return	bikeWorkSweepService.pageListById(id);
	 }
	/**
	 *   添加
	 *
	 * @param bikeWorkSweep
	 * @return
	 */
	@AutoLog(value = "保洁工单-添加")
	@ApiOperation(value="保洁工单-添加", notes="保洁工单-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody BikeWorkSweep bikeWorkSweep) {
		if (StringUtils.isEmpty(bikeWorkSweep.getStationCode())){
			return Result.error("请输入网点编号");
		}
		if (StringUtils.isEmpty(bikeWorkSweep.getDeviceCode())){
			return Result.error("请输入车桩编号");
		}
		String YN = bikeWorkRepairService.checkAddData(bikeWorkSweep.getStationCode(), bikeWorkSweep.getDeviceCode());
		if (YN.equals(BikeConstant.y_n_yes)){
			bikeWorkSweep.setCode(RandomCodeUtils.getRandomCode(BikeConstant.work_sweep_open));
			if (StringUtils.isNotEmpty(bikeWorkSweep.getIsDispatch())){
				if(bikeWorkSweep.getIsDispatch().equals(BikeConstant.y_n_yes)){
					bikeWorkSweep.setDispatchTime(new Date());
				}
			}
			bikeWorkSweepService.save(bikeWorkSweep);
			return Result.OK("添加成功！");
		}else {
			return Result.error(YN);
		}

	}

	 /**
	  *
	  *
	  * @param bikeWorkSweep
	  * @return
	  */
	 @AutoLog(value = "保洁-再次派单")
	 @ApiOperation(value="保洁-再次派单", notes="保洁-再次派单")
	 @PostMapping(value = "/addAgain")
	 public Result<?> addAgain(@RequestBody BikeWorkSweep bikeWorkSweep) {
		 //如果原工单为新增 再次派单来源为 原工单id（用于相关历史工单）
		 if (bikeWorkSweep.getWorkSource().equals("1")){
			 bikeWorkSweep.setSourceId(bikeWorkSweep.getCode());
		 }
		 //-----------需要修改
		 //是否解决
		 bikeWorkSweep.setIsSolve(BikeConstant.y_n_no);
		 //已派单
		 bikeWorkSweep.setIsDispatch(BikeConstant.y_n_yes);
		 //新转工单时间
		 bikeWorkSweep.setDispatchTime(new Date());
		 //新工单code
		 bikeWorkSweep.setCode(RandomCodeUtils.getRandomCode(BikeConstant.work_dispatch_open));
		 //来源为再次派单
		 bikeWorkSweep.setWorkSource("4");
		 //--------------需要删除
		 bikeWorkSweep.setContent(null);
		 bikeWorkSweep.setActualTime(null);
		 bikeWorkSweep.setActualAddress(null);
		 bikeWorkSweep.setCreateBy(null);
		 bikeWorkSweep.setCreateTime(null);
		 bikeWorkSweep.setUpdateBy(null);
		 bikeWorkSweep.setUpdateTime(null);
		 bikeWorkSweepService.save(bikeWorkSweep);

		 return Result.OK("添加成功！");
	 }
	/**
	 *  编辑
	 *
	 * @param bikeWorkSweep
	 * @return
	 */
	@AutoLog(value = "保洁工单-编辑",operateType = 3)
	@ApiOperation(value="保洁工单-编辑", notes="保洁工单-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody BikeWorkSweep bikeWorkSweep) {

		bikeWorkSweepService.updateById(bikeWorkSweep);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "保洁工单-通过id删除")
	@ApiOperation(value="保洁工单-通过id删除", notes="保洁工单-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		return bikeWorkSweepService.deleteById(id);
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "保洁工单-批量删除")
	@ApiOperation(value="保洁工单-批量删除", notes="保洁工单-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		String[] split = ids.split(",");
		//判断派单情况
		String yn = bikeWorkSweepService.judgeDelet(split);
		if (yn.equals(BikeConstant.y_n_yes)){
			this.bikeWorkSweepService.removeByIds(Arrays.asList(ids.split(",")));
			return Result.OK("批量删除成功!");
		}else {
			return Result.error("无法删除已派单工单");
		}
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="保洁工单-通过id查询", notes="保洁工单-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		BikeWorkSweep bikeWorkSweep = bikeWorkSweepService.getById(id);
		if(bikeWorkSweep==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(bikeWorkSweep);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param bikeWorkSweep
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BikeWorkSweep bikeWorkSweep) {
    	return bikeWorkSweepService.exportXls(bikeWorkSweep,request);
        //return super.exportXls(request, bikeWorkSweep, BikeWorkSweep.class, "保洁工单");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BikeWorkSweep.class);
    }

}
